package com.lottery.api.aop;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lottery.api.exception.MemberException;
import com.lottery.common.contants.Constants;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.entity.MemberInfo;
import com.lottery.common.entity.SysConfig;
import com.lottery.common.model.*;
import com.lottery.common.utils.MD5;
import com.lottery.common.utils.MyException;
import com.lottery.common.utils.RedisUtils;
import com.lottery.common.utils.XssUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author furutaka
 */
@Aspect
@Component
@Slf4j
public class CoerceLoginAopImpl {
	@Resource(name = "stringRedisTemplate")
	private StringRedisTemplate redisTemplate;

//	@Qualifier("redisTemplate")
//	@Autowired
//	private RedisTemplate  ss;


	@Pointcut("@annotation(com.lottery.api.annotation.CoerceLoginAop)")
	public void memberAop(){
	}

	@Before("memberAop()")
	public void before(JoinPoint joinPoint) throws MemberException {
//		MessageInfo messageInfo=new BaseModel();
		Object[]o=joinPoint.getArgs();
		HttpServletRequest request=(HttpServletRequest)o[0];
		String phone = request.getHeader("phone");
		String token = request.getHeader("token");
		log.info("用户校验 phone={} token = {}",phone,token);

		NutMap nutMap= RedisUtils.getLoginInfo(phone);
		if (nutMap==null|| !nutMap.getString("token").equals(token)){
			log.info("用户不存在 ");
			throw new MemberException("登录异常");
		}

		MemberInfo info=(MemberInfo)o[1];
		info.setId(nutMap.getLong("memberId"));
		info.setExpiredTime(nutMap.getTime("expiredTime"));
		info.setMemberIdentify(nutMap.getInt("identify"));
		info.setPhone(phone);
		info.setBaseToken(token);
//		info.setIsUpdateToken(Constants.TYPE_ONE);
//		Long time=System.currentTimeMillis();
//		if (time-nutMap.getLong("tokenTime")> Long.valueOf(RedisUtils.getConfigFromRedis("1801", SysConfig.class).getValue())){
//			token= MD5.md5(phone+ UUID.randomUUID().toString());
//			info.setBaseToken(token);
//			info.setUpdateTokenTime(time);
//			info.setIsUpdateToken(Constants.TYPE_ZERO);
//		}

		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
	}

	@After("memberAop()")
	public void after(JoinPoint joinPoint){

	}


	/**
	 * 后置通知
	 * @param ret
	 * @throws Throwable
	 */
	@AfterReturning(returning = "ret", pointcut = "memberAop()")
	public void afterReturning(JoinPoint joinPoint,Object ret) throws Throwable {
		// 处理完请求，返回内容
//		ReQueryResult result =(ReQueryResult)ret;
//		Object[]o=joinPoint.getArgs();
//		MemberInfo info=(MemberInfo)o[1];
//		result.setIsUpdateToken(Constants.TYPE_ONE);
//		if (info.getIsUpdateToken()==Constants.TYPE_ZERO){
//			result.setIsUpdateToken(Constants.TYPE_ZERO);
//			result.setToken(info.getBaseToken());
//			NutMap map=new NutMap();
//			map.setv("phone",info.getPhone());
//			map.setv("token",info.getBaseToken());
//			map.setv("tokenTime",info.getUpdateTokenTime());
//			map.setv("memberId",info.getId());
//			redisTemplate.opsForHash().put("loginMap",info.getPhone(),JSON.toJSONString(map));
//		}
//
//		if (result.getData()!=null){
////			NutMap nutMap=(NutMap)result.getData();
////			nutMap.setv("aop","aop");
////			result.setData(nutMap);
//		}
//
//		System.out.println(ret);
	}

}
